﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AjaxDataTableEdit.ascx.cs" Inherits="AjaxDataTableEdit" %>

<link href="Style/EditStyle1.css" rel="stylesheet" type="text/css" />

<script src="JavaScript/jquery.js" type="text/javascript"></script>
<script src="JavaScript/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="JavaScript/ajaxupload.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
//post的页面
var _AjaxTableEditPostPage="<%= _AjaxTableEditPostPage %>";
//要(增加/修改)的表名
var _AjaxTableName="<%= _AjaxTableName %>";
//表字段的相关信息
var _AjaxTableFieldInfo=new Array();



//是增加还是修改
var _isAdd="<%= _isAdd %>"=="True"?true:false;
//要修改的关键字ID
var _AjaxTableEditID="<%= _AjaxTableEditID %>";
//表的主建
var _AjaxTableKey="<%= _AjaxTableKey %>";
//主键是否是自增长类型
var _AjaxTableKeyIsInt="<%= _AjaxTableKeyIsInt %>";
//要修改的记录信息数据
var _AjaxEditJsonInfo=new Array();


$(document).ready(function(){
    _DownTableFieldInfoArray();
});

//得到表字段的相关信息 fieldName,fieldNameCh,editTypeID,regexTypeID,regexText,keyID,minLength,maxLength,isIntType
function _DownTableFieldInfoArray()
{
    $.ajax({
        url:_AjaxTableEditPostPage,
        type:"POST",
        dataType:"json",
        data:{_type:"E_DownTableFieldInfo",_TableName:_AjaxTableName},
        success:function(data){
            _AjaxTableFieldInfo=data;
            _BindTableFieldInfo();
        }
    });
}
//绑定字段信息 fieldName,fieldNameCh,editTypeID,regexTypeID,regexText,keyID,minLength,maxLength,isIntType
function _BindTableFieldInfo()
{
    var FieldHtmlStr="";
    for(var i=0;i<_AjaxTableFieldInfo.TableInfo.length;i++)
    {
        FieldHtmlStr+="<div class=\"trstyle\">";
        switch(_AjaxTableFieldInfo.TableInfo[i]["editTypeID"])
        {
            case "1":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><input class=\"FieldInput_CSS\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\" maxlength=\""+_AjaxTableFieldInfo.TableInfo[i]["maxLength"]+"\" minlength=\""+_AjaxTableFieldInfo.TableInfo[i]["minLength"]+"\" type=\"text\" value=\"\" onfocus=\"_ControlsFocus('"+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"')\" onblur=\"_ControlsBlur('"+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"','"+_AjaxTableFieldInfo.TableInfo[i]["regexTypeID"]+"')\" /></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
                break;
            case "2":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><input class=\"FieldInput_CSS\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\" minlength=\""+_AjaxTableFieldInfo.TableInfo[i]["minLength"]+"\" type=\"text\" value=\"\" readonly=\"readonly\" onfocus=\"WdatePicker({skin:'whyGreen',isShowClear:false,dateFmt:'yyyy-MM-dd'})\" /></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
                break;
            case "3":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><select class=\"FieldSelect_CSS\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\" onfocus=\"_ControlsFocus('"+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"')\">";
                var SelectKeyIDStr=_AjaxTableFieldInfo.TableInfo[i]["keyID"];
                $.ajax({
                    url:_AjaxTableEditPostPage,
                    dataType:"json",
                    async:false,
                    type:"POST",
                    data:{_type:"E_DownDDLNameVal",keyID:SelectKeyIDStr},
                    success:function(data){
                        for(var j=0;j<data.TableInfo.length;j++)
                        {
                            FieldHtmlStr+="<option value=\""+data.TableInfo[j]["keyVal"]+"\">"+data.TableInfo[j]["keyNameCh"]+"</option>";
                        }
                    }
                });
                FieldHtmlStr+="</select></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
                break;
            case "4":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><input class=\"FieldInput_CSS\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\" maxlength=\""+_AjaxTableFieldInfo.TableInfo[i]["maxLength"]+"\" minlength=\""+_AjaxTableFieldInfo.TableInfo[i]["minLength"]+"\" type=\"password\" value=\"\" onfocus=\"_ControlsFocus('"+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"')\" onblur=\"_ControlsBlur('"+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"','"+_AjaxTableFieldInfo.TableInfo[i]["regexTypeID"]+"')\" /></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
                break;
            case "5":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><input class=\"FieldFileText_CSS\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\" minlength=\""+_AjaxTableFieldInfo.TableInfo[i]["minLength"]+"\" type=\"text\" value=\"\" readonly=\"readonly\" /><input fieldbtn=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" regexid=\""+_AjaxTableFieldInfo.TableInfo[i]["regexTypeID"]+"\" class=\"FieldFileBtn_CSS\" type=\"button\" value=\"浏览...\" /></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
                break;
            case "6":
                FieldHtmlStr+="<span class=\"FieldOneSpan_CSS\">"+_AjaxTableFieldInfo.TableInfo[i]["fieldNameCh"]+":</span>";
                FieldHtmlStr+="<span class=\"textsty\"><textarea class=\"FieldTextArea_CSS\" cols=\"25\" rows=\"5\" field=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\" isint=\""+_AjaxTableFieldInfo.TableInfo[i]["isIntType"]+"\"></textarea></span>";
                FieldHtmlStr+="<span class=\"FieldTwoSpan_CSS\" fieldspan=\""+_AjaxTableFieldInfo.TableInfo[i]["fieldName"]+"\">"+_AjaxTableFieldInfo.TableInfo[i]["regexText"]+"</span>";
            default :
                break;
        }
        FieldHtmlStr+="</div>";
    }
    $("#AjaxEditField_div").html(FieldHtmlStr);
    //绑定上传文件
    _BindUpFileBtn();
    
    //绑定编辑信息
    if(!_isAdd)
    {
        _DownEditInto();
    }
}
//当控件得到焦点的时候
function _ControlsFocus(fieldStr)
{
    $("#AjaxEditField_div span[field='"+fieldStr+"']").attr("class","FieldTwoSpan_CSS");
}
//当控件失去焦点的时候
function _ControlsBlur(fieldStr,regexTypeID)
{
    //没有验证
    if(regexTypeID=="")
    {
        $("#AjaxEditField_div span[fieldspan='"+fieldStr+"']").attr("class","FieldTwoSpanTrue_CSS");
        return;
    }
    var valStr=$("#AjaxEditField_div input[field='"+fieldStr+"']").val();
    var inputMinLength=$("#AjaxEditField_div input[field='"+fieldStr+"']").attr("minlength");
    //当输入的值小于最小输入长度时候
    if(valStr.length < inputMinLength)
    {
        $("#AjaxEditField_div span[fieldspan='"+fieldStr+"']").attr("class","FieldTwoSpanError_CSS");
        return;
    }
    $.post(_AjaxTableEditPostPage,{_type:"E_regexType",regexID:regexTypeID},function(data){
        var reg=new RegExp(data.toString());
        var regIsTrue=reg.test(valStr);
        if(!regIsTrue)
        {
            $("#AjaxEditField_div span[fieldspan='"+fieldStr+"']").attr("class","FieldTwoSpanError_CSS");
        }else
        {
            $("#AjaxEditField_div span[fieldspan='"+fieldStr+"']").attr("class","FieldTwoSpanTrue_CSS");
        }
    });
}
//绑定上传文件组件
function _BindUpFileBtn()
{
    $("#AjaxEditField_div input[type='button'][class='FieldFileBtn_CSS']").each(function(){
        var fieldStr=this.fieldbtn;
        var regexid=this.regexid;
        new AjaxUpload(this,{
            action:_AjaxTableEditPostPage,
            data:{_type:"E_UpFile"},
            autoSubmit:true,
            onComplete:function(file, response){
                $("#AjaxEditField_div input[class='FieldFileText_CSS'][field='"+fieldStr+"']").val($(response).text());
                //对上传文件进行验证
                _ControlsBlur(fieldStr,regexid);
            }
        });
    });
}
</script>
<script language="javascript" type="text/javascript">

//下载要修改的信息
function _DownEditInto()
{
    if(_AjaxTableEditID.length==0 || _AjaxTableKey.length==0)
    {
        alert("信息不全");
        return;
    }
    $.ajax({
        url:_AjaxTableEditPostPage,
        type:"POST",
        dataType:"json",
        data:{_type:"E_DownEditInto",_TableName:_AjaxTableName,_TableKey:_AjaxTableKey,_EditID:_AjaxTableEditID,_TableKeyIsInt:_AjaxTableKeyIsInt},
        success:function(data){
            _AjaxEditJsonInfo=data;
            //绑定信息
            _BindEditInto();
        }
    });
}
//绑定修改信息
function _BindEditInto()
{
    //绑定文本框,密码框,日期框,上传文件框
    $("#AjaxEditField_div input[field]").each(function(){
        this.value=_AjaxEditJsonInfo.TableInfo[0][this.field];
    });
    //绑定下拉框
    $("#AjaxEditField_div select[field]").each(function(){
        this.value=_AjaxEditJsonInfo.TableInfo[0][this.field];
    });
    //绑定多行文本框
    $("#AjaxEditField_div textarea[field]").each(function(){
        this.value=_AjaxEditJsonInfo.TableInfo[0][this.field];
    });
}
</script>

<script language="javascript" type="text/javascript">
//发送一条SQL语句
function PostSqlExec(sqlStr)
{
    $.post(_AjaxTableEditPostPage,{_type:"E_ExSql",SSql:sqlStr},function(data){
        alert(data.toString());
        if(data=="true")
        {
            return true;
        }else
        {
            return false;
        }
    });
}
//点击提交的时候如果是增加
function BtnInsert_OK_Click()
{
    var E_sqlStr="insert into "+_AjaxTableName+"(";
    var E_fieldStr="";
    var E_valStr="";
    //fieldName,fieldNameCh,editTypeID,regexTypeID,regexText,keyID,minLength,maxLength,isIntType
    for(var i=0;i<_AjaxTableFieldInfo.TableInfo.length;i++)
    {
        var fieldName=_AjaxTableFieldInfo.TableInfo[i]["fieldName"];
        E_fieldStr+=fieldName+",";
        var E_editType=_AjaxTableFieldInfo.TableInfo[i]["editTypeID"];
        switch(E_editType)
        {
            case "1":
            case "2":
            case "4":
            case "5":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=$("#AjaxEditField_div input[field='"+fieldName+"']").val();
                    E_valStr+="'"+E_CVal+"',";
                }else
                {
                    var E_CVal=$("#AjaxEditField_div input[field='"+fieldName+"']").val();
                    E_valStr+=E_CVal+",";
                }
                break;
            case "3":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=$("#AjaxEditField_div select[field='"+fieldName+"']").val();
                    E_valStr+="'"+E_CVal+"',";
                }else
                {
                    var E_CVal=$("#AjaxEditField_div select[field='"+fieldName+"']").val();
                    E_valStr+=E_CVal+",";
                }
                break;
            case "6":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=$("#AjaxEditField_div textarea[field='"+fieldName+"']").val();
                    E_valStr+="'"+E_CVal+"',";
                }else
                {
                    var E_CVal=$("#AjaxEditField_div textarea[field='"+fieldName+"']").val();
                    E_valStr+=E_CVal+",";
                }
                break;
            default :break;
        }
    }
    E_fieldStr=E_fieldStr.substring(0,E_fieldStr.length-1);
    E_valStr=E_valStr.substring(0,E_valStr.length-1);
    E_sqlStr=E_sqlStr+E_fieldStr+") values("+E_valStr+")";
    //alert(E_sqlStr);
    $.post(_AjaxTableEditPostPage,{_type:"E_ExSql",SSql:E_sqlStr},function(data){
        if(data.toString()=="true")
        {
            alert("插入成功");
            location.href="Default.aspx";
        }else
        {
            alert("插入失败");
            location.href="Default.aspx";
        }
    });
}
//点击提交的时候如果是修改fieldName,fieldNameCh,editTypeID,regexTypeID,regexText,keyID,minLength,maxLength,isIntType
function BtnUpdate_OK_click()
{
    var E_sqlStr="update "+_AjaxTableName+" set ";
    for(var i=0;i<_AjaxTableFieldInfo.TableInfo.length;i++)
    {
        var fieldName=_AjaxTableFieldInfo.TableInfo[i]["fieldName"];
        var E_editType=_AjaxTableFieldInfo.TableInfo[i]["editTypeID"];
        switch(E_editType)
        {
            case "1":
            case "2":
            case "4":
            case "5":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div input[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"='"+E_CVal+"',";
                }else
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div input[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"="+E_CVal+",";
                }
                break;
            case "3":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div select[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"='"+E_CVal+"',";
                }else
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div select[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"="+E_CVal+",";
                }
                break;
            case "6":
                if(_AjaxTableFieldInfo.TableInfo[i]["isIntType"]=="0")
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div textarea[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"='"+E_CVal+"',";
                }else
                {
                    var E_CVal=E_CVal=$("#AjaxEditField_div textarea[field='"+fieldName+"']").val();
                    E_sqlStr+=fieldName+"="+E_CVal+",";
                }
                break;
            default :break;
        }
    }
    E_sqlStr=E_sqlStr.substring(0,E_sqlStr.length-1)+" where "+_AjaxTableKey+"=";
    if(_AjaxTableKeyIsInt=="0")
    {
        E_sqlStr+="'"+_AjaxTableEditID+"'";
    }else
    {
        E_sqlStr+=_AjaxTableEditID;
    }
    //alert(E_sqlStr);
    $.post(_AjaxTableEditPostPage,{_type:"E_ExSql",SSql:E_sqlStr},function(data){
        if(data.toString()=="true")
        {
            alert("修改成功");
            location.href="Default.aspx";
        }else
        {
            alert("修改失败");
            location.href="Default.aspx";
        }
    });
}
//提交点击
function BtnOK_click()
{
    if(!_isAdd)
    {
        BtnUpdate_OK_click();
    }else
    {
        BtnInsert_OK_Click();
    }
}
</script>


<div>
    <div id="AjaxDataTableEdit_div">
        <div id="AjaxEditField_div">
        </div>
        <div id="AjaxEditBtn_div">
            <input type="button" value="提交" onclick="BtnOK_click()" />
        </div>
    </div>
</div>